package com.sgb.kjwl.view.ui.fragment.material;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentMerchantRecommendationBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.BossEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.MaterialEvent;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.adapter.home.MerchantRecommendationAdapter;
import com.sgb.kjwl.view.ui.fragment.home.LazyFragment2;
import com.sgb.kjwl.viewmodel.home.MerchantRecommendationModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/***
 * @author Administrator
 * by hq 2021.04.01
 * App首页模块的商品界面中的商家推荐
 */
public class MerchantRecommendationFragment extends LazyFragment2 {

    @Inject
    MerchantRecommendationModel model;

    private boolean isRefresh = false;
    private int position = 0;

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public boolean isRefresh() {
        return isRefresh;
    }

    public void setRefresh(boolean refresh) {
        isRefresh = refresh;
    }

    private FragmentMerchantRecommendationBinding binding;
    private MerchantRecommendationAdapter mMerchantRecommendationAdapter;

    private double longitude = 0.0d;
    private double latitude = 0.0d;
    private final List<BossEntity> bossData = new ArrayList<>();
    private boolean isOnResumed = false;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    public static MerchantRecommendationFragment newInstance() {
        MerchantRecommendationFragment fragment = new MerchantRecommendationFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected void onMultiClick(View v) {

    }

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_merchant_recommendation, container, false);
        return binding.getRoot();
    }

    /***
     * 初始化组件
     */
    @Override
    protected void initView() {
        if (mMerchantRecommendationAdapter == null) {
            mMerchantRecommendationAdapter = new MerchantRecommendationAdapter(getActivity(), null);
            mMerchantRecommendationAdapter.setOnItemClick(new AdapterOnItemClick<BossEntity>() {
                @Override
                public void onItemClick(BossEntity bossEntity, int position) {
                    WebsiteInfoActivity.start(getActivity(), bossEntity.getNo(), WebsiteInfoActivity.WEBSITE_FOR_COMPANY, 0, 0);
                }
            });
        }
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.setAdapter(mMerchantRecommendationAdapter);
        binding.recyclerView.setPullRefreshEnabled(false);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //刷新
            }

            @Override
            public void onLoadMore() {
                //加载更多
                model.getRecommendSupplier(longitude, latitude, true);
            }
        });
        isOnResumed = true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(MaterialEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_REFRESH_FRAGMENT) {
            if (isOnResumed && model != null) {
                model.getRecommendSupplier(longitude, latitude, false);
            }
        }
    }

    /****
     * 获取数据
     */
    @Override
    protected void loadData() {
        String longitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE, "");
        String latitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE, "");
        if ("".equals(longitude) || "".equals(latitude)) {
            model.getLocationLL();
        } else {
            this.longitude = Double.parseDouble(longitude);
            this.latitude = Double.parseDouble(latitude);
            model.getRecommendSupplier(Double.parseDouble(longitude), Double.parseDouble(latitude), false);
        }
    }

    /***
     * 获取经纬度信息
     * @param data 经纬度信息
     */
    public void getLocationLL(MapLongAndLatEntity data) {
        if (null == data) {
            binding.rlEmpty.setVisibility(View.VISIBLE);
        } else {
            this.longitude = Double.parseDouble(data.getLng());
            this.latitude = Double.parseDouble(data.getLat());
            model.getRecommendSupplier(Double.parseDouble(data.getLng()), Double.parseDouble(data.getLat()), false);
        }
    }

    /***
     * 商家列表信息--成功
     */
    public void showMerChantList(List<BossEntity> data, boolean isLoadMore) {

        if (isLoadMore) {
            if (data.size() > 0) {
                binding.recyclerView.loadMoreComplete();
                mMerchantRecommendationAdapter.addDatas(data);
            } else {
                binding.recyclerView.setNoMore(true);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (null != data && data.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.rlEmpty.setVisibility(View.GONE);
                mMerchantRecommendationAdapter.setDatas(data);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                mMerchantRecommendationAdapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.rlEmpty.setVisibility(View.VISIBLE);
            }
        }

    }

    /***
     * 商家列表信息--失败
     */
    @Deprecated
    public void setErrorMessage() {
        binding.rlEmpty.setVisibility(View.VISIBLE);
        binding.recyclerView.setVisibility(View.GONE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
